home *** CD-ROM | disk | FTP | other *** search
/ Aminet 40 / Aminet 40 (2000)(Schatztruhe)[!][Dec 2000].iso / Aminet / dev / lang / Python16.lha / Python-1.6 / Lib / Python1.6 / distutils / command / build_clib.py < prev    next >
Encoding:
Python Source  |  2000-06-25  |  7.9 KB  |  223 lines

  1. """distutils.command.build_clib
  2.  
  3. Implements the Distutils 'build_clib' command, to build a C/C++ library
  4. that is included in the module distribution and needed by an extension
  5. module."""
  6.  
  7. # created (an empty husk) 1999/12/18, Greg Ward
  8. # fleshed out 2000/02/03-04
  9.  
  10. __revision__ = "$Id: build_clib.py,v 1.19 2000/06/25 02:10:58 gward Exp $"
  11.  
  12.  
  13. # XXX this module has *lots* of code ripped-off quite transparently from
  14. # build_ext.py -- not surprisingly really, as the work required to build
  15. # a static library from a collection of C source files is not really all
  16. # that different from what's required to build a shared object file from
  17. # a collection of C source files.  Nevertheless, I haven't done the
  18. # necessary refactoring to account for the overlap in code between the
  19. # two modules, mainly because a number of subtle details changed in the
  20. # cut 'n paste.  Sigh.
  21.  
  22. import os, string
  23. from types import *
  24. from distutils.core import Command
  25. from distutils.errors import *
  26. from distutils.sysconfig import customize_compiler
  27.  
  28.  
  29. def show_compilers ():
  30.     from distutils.ccompiler import show_compilers
  31.     show_compilers()
  32.  
  33.  
  34. class build_clib (Command):
  35.  
  36.     description = "build C/C++ libraries used by Python extensions"
  37.  
  38.     user_options = [
  39.         ('build-clib', 'b',
  40.          "directory to build C/C++ libraries to"),
  41.         ('build-temp', 't',
  42.          "directory to put temporary build by-products"),
  43.         ('debug', 'g',
  44.          "compile with debugging information"),
  45.         ('force', 'f',
  46.          "forcibly build everything (ignore file timestamps)"),
  47.         ('compiler=', 'c',
  48.          "specify the compiler type"),
  49.         ]
  50.  
  51.     help_options = [
  52.         ('help-compiler', None,
  53.          "list available compilers", show_compilers),
  54.     ]
  55.  
  56.     def initialize_options (self):
  57.         self.build_clib = None
  58.         self.build_temp = None
  59.  
  60.         # List of libraries to build
  61.         self.libraries = None
  62.  
  63.         # Compilation options for all libraries
  64.         self.include_dirs = None
  65.         self.define = None
  66.         self.undef = None
  67.         self.debug = None
  68.         self.force = 0
  69.         self.compiler = None
  70.  
  71.     # initialize_options()
  72.  
  73.  
  74.     def finalize_options (self):
  75.  
  76.         # This might be confusing: both build-clib and build-temp default
  77.         # to build-temp as defined by the "build" command.  This is because
  78.         # I think that C libraries are really just temporary build
  79.         # by-products, at least from the point of view of building Python
  80.         # extensions -- but I want to keep my options open.
  81.         self.set_undefined_options ('build',
  82.                                     ('build_temp', 'build_clib'),
  83.                                     ('build_temp', 'build_temp'),
  84.                                     ('compiler', 'compiler'),
  85.                                     ('debug', 'debug'),
  86.                                     ('force', 'force'))
  87.  
  88.         self.libraries = self.distribution.libraries
  89.         if self.libraries:
  90.             self.check_library_list (self.libraries)
  91.  
  92.         if self.include_dirs is None:
  93.             self.include_dirs = self.distribution.include_dirs or []
  94.         if type (self.include_dirs) is StringType:
  95.             self.include_dirs = string.split (self.include_dirs,
  96.                                               os.pathsep)
  97.  
  98.         # XXX same as for build_ext -- what about 'self.define' and
  99.         # 'self.undef' ?
  100.  
  101.     # finalize_options()
  102.  
  103.  
  104.     def run (self):
  105.  
  106.         if not self.libraries:
  107.             return
  108.  
  109.         # Yech -- this is cut 'n pasted from build_ext.py!
  110.         from distutils.ccompiler import new_compiler
  111.         self.compiler = new_compiler (compiler=self.compiler,
  112.                                       verbose=self.verbose,
  113.                                       dry_run=self.dry_run,
  114.                                       force=self.force)
  115.         customize_compiler(self.compiler)
  116.  
  117.         if self.include_dirs is not None:
  118.             self.compiler.set_include_dirs (self.include_dirs)
  119.         if self.define is not None:
  120.             # 'define' option is a list of (name,value) tuples
  121.             for (name,value) in self.define:
  122.                 self.compiler.define_macro (name, value)
  123.         if self.undef is not None:
  124.             for macro in self.undef:
  125.                 self.compiler.undefine_macro (macro)
  126.  
  127.         self.build_libraries (self.libraries)
  128.  
  129.     # run()
  130.  
  131.  
  132.     def check_library_list (self, libraries):
  133.         """Ensure that the list of libraries (presumably provided as a
  134.            command option 'libraries') is valid, i.e. it is a list of
  135.            2-tuples, where the tuples are (library_name, build_info_dict).
  136.            Raise DistutilsSetupError if the structure is invalid anywhere;
  137.            just returns otherwise."""
  138.  
  139.         # Yechh, blecch, ackk: this is ripped straight out of build_ext.py,
  140.         # with only names changed to protect the innocent!
  141.  
  142.         if type (libraries) is not ListType:
  143.             raise DistutilsSetupError, \
  144.                   "'libraries' option must be a list of tuples"
  145.  
  146.         for lib in libraries:
  147.             if type (lib) is not TupleType and len (lib) != 2:
  148.                 raise DistutilsSetupError, \
  149.                       "each element of 'libraries' must a 2-tuple"
  150.  
  151.             if type (lib[0]) is not StringType:
  152.                 raise DistutilsSetupError, \
  153.                       "first element of each tuple in 'libraries' " + \
  154.                       "must be a string (the library name)"
  155.             if '/' in lib[0] or (os.sep != '/' and os.sep in lib[0]):
  156.                 raise DistutilsSetupError, \
  157.                       ("bad library name '%s': " + 
  158.                        "may not contain directory separators") % \
  159.                       lib[0]
  160.  
  161.             if type (lib[1]) is not DictionaryType:
  162.                 raise DistutilsSetupError, \
  163.                       "second element of each tuple in 'libraries' " + \
  164.                       "must be a dictionary (build info)"
  165.         # for lib
  166.  
  167.     # check_library_list ()
  168.  
  169.  
  170.     def get_library_names (self):
  171.         # Assume the library list is valid -- 'check_library_list()' is
  172.         # called from 'finalize_options()', so it should be!
  173.  
  174.         if not self.libraries:
  175.             return None
  176.  
  177.         lib_names = []
  178.         for (lib_name, build_info) in self.libraries:
  179.             lib_names.append (lib_name)
  180.         return lib_names
  181.  
  182.     # get_library_names ()
  183.  
  184.  
  185.     def build_libraries (self, libraries):
  186.  
  187.         compiler = self.compiler
  188.  
  189.         for (lib_name, build_info) in libraries:
  190.             sources = build_info.get ('sources')
  191.             if sources is None or type (sources) not in (ListType, TupleType):
  192.                 raise DistutilsSetupError, \
  193.                       ("in 'libraries' option (library '%s'), " +
  194.                        "'sources' must be present and must be " +
  195.                        "a list of source filenames") % lib_name
  196.             sources = list (sources)
  197.  
  198.             self.announce ("building '%s' library" % lib_name)
  199.  
  200.             # First, compile the source code to object files in the library
  201.             # directory.  (This should probably change to putting object
  202.             # files in a temporary build directory.)
  203.             macros = build_info.get ('macros')
  204.             include_dirs = build_info.get ('include_dirs')
  205.             objects = self.compiler.compile (sources,
  206.                                              output_dir=self.build_temp,
  207.                                              macros=macros,
  208.                                              include_dirs=include_dirs,
  209.                                              debug=self.debug)
  210.  
  211.             # Now "link" the object files together into a static library.
  212.             # (On Unix at least, this isn't really linking -- it just
  213.             # builds an archive.  Whatever.)
  214.             self.compiler.create_static_lib (objects, lib_name,
  215.                                              output_dir=self.build_clib,
  216.                                              debug=self.debug)
  217.  
  218.         # for libraries
  219.  
  220.     # build_libraries ()
  221.  
  222. # class build_lib
  223.